package com.yanqu.road.server.pb.chefduel;

import com.yanqu.road.entity.activity.chefduel.ChefDuelMatch;
import com.yanqu.road.entity.activity.chefduel.ChefDuelMatchDish;
import com.yanqu.road.entity.activity.chefduel.ChefDuelMatchReward;
import com.yanqu.road.entity.activity.chefduel.process2.*;
import com.yanqu.road.entity.activity.chefduel.process3.ChefDuel3Table;
import com.yanqu.road.entity.enums.eChefDuelFoodStatusType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.manager.activity.relation.CrossRelationRankMgr;
import com.yanqu.road.server.manager.chefduel.CrossChefDuelActivityMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CrossChefDuelPb {

    public static ChefDuelProto.ChefDuel3UserRewardSyncMsg.Builder parseChefDuel3UserRewardSyncMsg(int activityId, Map<Long, ChefDuelUpUser> cookerMap, Map<Long,Map<Integer, ChefDuel3Table>> cookBoyTableMap){
        ChefDuelProto.ChefDuel3UserRewardSyncMsg.Builder msg = ChefDuelProto.ChefDuel3UserRewardSyncMsg.newBuilder();
        msg.setActivityId(activityId);
        for(ChefDuelUpUser cookBoyUpUser : new ArrayList<>(cookerMap.values())){
            ChefDuelProto.ChefDuel3UserRewardTempMsg.Builder userMsg = ChefDuelProto.ChefDuel3UserRewardTempMsg.newBuilder();
            userMsg.setUserId(cookBoyUpUser.getUserId());
            if(cookBoyTableMap.containsKey(cookBoyUpUser.getUserId())){
                int userCount = 0;
                for (ChefDuel3Table cookBoy3Table : new ArrayList<>(cookBoyTableMap.get(cookBoyUpUser.getUserId()).values())){
                    userCount += cookBoy3Table.getChefDuel3JoinerMap().size();
                }
                userMsg.setUserCount(userCount);
                userMsg.setBanquetCount(cookBoyTableMap.get(cookBoyUpUser.getUserId()).size());
            }else{
                userMsg.setBanquetCount(0);
                userMsg.setUserCount(0);
            }
            msg.addDataList(userMsg);
        }
        return msg;
    }

    public static ChefDuelProto.CrossChefDuelWindUpMsg.Builder parseCrossChefDuelWindUpMsg(int activityId, List<ChefDuelProto.CrossChefDuelWindUpCookerTempMsg> userIdList){
        ChefDuelProto.CrossChefDuelWindUpMsg.Builder respMsg = ChefDuelProto.CrossChefDuelWindUpMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.addAllUserIdList(userIdList);
        return respMsg;
    }

    public static ChefDuelProto.CrossChefDuel2HorseSyncServerMsg.Builder parseChefDuel2HorseSyncServerMsg(List<HorseRaceLamp> syncList, int activityId){
        ChefDuelProto.CrossChefDuel2HorseSyncServerMsg.Builder respMsg = ChefDuelProto.CrossChefDuel2HorseSyncServerMsg.newBuilder();
        respMsg.setActivityId(activityId);
        for(HorseRaceLamp horseRaceLamp : syncList){
            ChefDuelProto.CrossChefDuel2HorseNoticeMsg.Builder tempMsg = ChefDuelProto.CrossChefDuel2HorseNoticeMsg.newBuilder();
            tempMsg.setActivityId(activityId);
            tempMsg.setUserId(horseRaceLamp.getUserId());
            tempMsg.setDishId(horseRaceLamp.getDishId());
            tempMsg.setTime(horseRaceLamp.getTime());
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(horseRaceLamp.getUserId());
            if(userBaseInfo == null){
                return null;
            }
            tempMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            respMsg.addNoticeList(tempMsg);
        }
        return respMsg;
    }

    public static ChefDuelProto.CrossChefDuel2TableSyncMsg.Builder parseCrossChefDuel2TableSyncMsg(Map<Integer, ChefDuel2DishTable> tableMap, int activityId, long serverId, long homeUserId, int dishNum,long integral){
        ChefDuelProto.CrossChefDuel2TableSyncMsg.Builder respMsg = ChefDuelProto.CrossChefDuel2TableSyncMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setUserId(homeUserId);
        ChefDuelProto.ChefDuel2UserIdTableRespMsg.Builder tableMsg = parseChefDuel2CookerTableRespMsg(tableMap,activityId,serverId,homeUserId,dishNum,integral);
        CrossChefDuelActivityMgr.setGoldEastLimit(homeUserId, tableMsg, activityId);
        CrossChefDuelActivityMgr.addSettingSpoon(homeUserId, tableMsg, activityId);
        respMsg.setUserTable(tableMsg);
        return respMsg;
    }

    public static ChefDuelProto.ChefDuel2TableTempMsg.Builder parseChefDuel2TableTempMsg(ChefDuel2DishTable cookBoy2DishTable){
        ChefDuelProto.ChefDuel2TableTempMsg.Builder tempTableMsg = ChefDuelProto.ChefDuel2TableTempMsg.newBuilder();
        tempTableMsg.setTableId(cookBoy2DishTable.getNo());
        tempTableMsg.setId(cookBoy2DishTable.getUserDishId());
        tempTableMsg.setOpenTime((int)(cookBoy2DishTable.getOpenTime()/1000));
        tempTableMsg.setOverTime((int)(cookBoy2DishTable.getOverTime()/1000));
        tempTableMsg.setDishId(cookBoy2DishTable.getDishId());
        Map<Long, ChefDuel2Joiner> joinerMap = cookBoy2DishTable.getChefDuel2JoinerMap();
        for (Map.Entry<Long, ChefDuel2Joiner> entry : joinerMap.entrySet()) {
            ChefDuel2Joiner cookBoy2Joiner = entry.getValue();
            ChefDuelProto.ChefDuel2TableJoinerTempMsg.Builder tempJoinerMsg = ChefDuelProto.ChefDuel2TableJoinerTempMsg.newBuilder();
            tempJoinerMsg.setServerId(cookBoy2Joiner.getServerId());
            ChefDuel2Spoon cookBoy2Spoon = CrossChefDuelActivityMgr.getChefDuel2Spoon(cookBoy2DishTable.getActivityId(),cookBoy2Joiner.getGifttId());
            if(cookBoy2Joiner.getRobotId() == 0){
                tempJoinerMsg.setUserId(cookBoy2Joiner.getUserId());
                tempJoinerMsg.setIsRobot(false);
                tempJoinerMsg.setIntegral(cookBoy2Spoon.getUserIntegral());
            }else{
                tempJoinerMsg.setUserId(cookBoy2Joiner.getRobotId());
                tempJoinerMsg.setIsRobot(true);
                tempJoinerMsg.setIntegral(cookBoy2Spoon.getUserIntegral() * CrossChefDuelActivityMgr.getChefDuel2RobotAdd(cookBoy2DishTable.getActivityId()) / 1000);
            }
            tempJoinerMsg.setSpoonId(cookBoy2Joiner.getGifttId());
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(cookBoy2Joiner.getUserId());
            if(userBaseInfo != null){
                tempJoinerMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            }else{
                tempJoinerMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(new UserBaseInfo()));
            }
            tempTableMsg.addJoinerList(tempJoinerMsg);
        }
        return tempTableMsg;
    }

    public static ChefDuelProto.ChefDuel2UserIdTableRespMsg.Builder parseChefDuel2CookerTableRespMsg(Map<Integer, ChefDuel2DishTable> tableMap, int activityId, long serverId, long homeUserId,int dishNum,long addIntegral){
        CrossUserRank crossUserRank = CrossRelationRankMgr.getCrossActivityRankInfo(activityId,serverId,homeUserId);
        long integral = 0;
        if(crossUserRank != null){
            integral = crossUserRank.getValue().longValue();
        }
        int rank = CrossRelationRankMgr.getCrossActivityRank(activityId,serverId,homeUserId);
        ChefDuelProto.ChefDuel2UserIdTableRespMsg.Builder respMsg = ChefDuelProto.ChefDuel2UserIdTableRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setIntegral(integral + addIntegral);
        respMsg.setRank(rank);
        respMsg.setDishNum(dishNum);
        for (Map.Entry<Integer, ChefDuel2DishTable> entry : tableMap.entrySet()) {
            ChefDuelProto.ChefDuel2TableTempMsg.Builder tempTableMsg = parseChefDuel2TableTempMsg(entry.getValue());
            respMsg.addTableList(tempTableMsg);
        }
        return respMsg;
    }

    public static ChefDuelProto.ChefDuel2UserTableInfoRespMsg.Builder parseChefDuel2RecordRespMsg(ChefDuel2DishTable cookBoy2DishTable) {
        ChefDuelProto.ChefDuel2UserTableInfoRespMsg.Builder respMsg = ChefDuelProto.ChefDuel2UserTableInfoRespMsg.newBuilder();
        respMsg.setRet(0);
        if(cookBoy2DishTable == null){
            respMsg.setRet(GameErrorCode.E_COOK_BOY2_TABLE_NOT_FOUND);
            return respMsg;
        }
        ChefDuelProto.ChefDuel2TableTempMsg.Builder tempMsg = parseChefDuel2TableTempMsg(cookBoy2DishTable);
        respMsg.setTable(tempMsg);
        return respMsg;
    }
}
